{{ header }}{{ column_left }}
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="float-end">
        <button type="submit" form="form-api" data-bs-toggle="tooltip" title="{{ button_save }}" class="btn btn-primary"><i class="fas fa-save"></i></button>
        <a href="{{ back }}" data-bs-toggle="tooltip" title="{{ button_back }}" class="btn btn-light"><i class="fas fa-reply"></i></a></div>
      <h1>{{ heading_title }}</h1>
      <ol class="breadcrumb">
        {% for breadcrumb in breadcrumbs %}
          <li class="breadcrumb-item"><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
        {% endfor %}
      </ol>
    </div>
  </div>
  <div class="container-fluid">
    <div class="card">
      <div class="card-header"><i class="fas fa-pencil-alt"></i> {{ text_form }}</div>
      <div class="card-body">
        <form id="form-api" action="{{ save }}" method="post" data-oc-toggle="ajax">
          <ul class="nav nav-tabs">
            <li class="nav-item"><a href="#tab-general" data-bs-toggle="tab" class="nav-link active">{{ tab_general }}</a></li>
            <li class="nav-item"><a href="#tab-ip" data-bs-toggle="tab" class="nav-link">{{ tab_ip }}</a></li>
            <li class="nav-item"><a href="#tab-session" data-bs-toggle="tab" class="nav-link">{{ tab_session }}</a></li>
          </ul>
          <div class="tab-content">
            <div id="tab-general" class="tab-pane active">
              <div class="row mb-3 required">
                <label for="input-username" class="col-sm-2 col-form-label">{{ entry_username }}</label>
                <div class="col-sm-10">
                  <input type="text" name="username" value="{{ username }}" placeholder="{{ entry_username }}" id="input-username" class="form-control"/>
                  <div id="error-username" class="invalid-feedback"></div>
                </div>
              </div>
              <div class="row mb-3 required">
                <label for="input-key" class="col-sm-2 col-form-label">{{ entry_key }}</label>
                <div class="col-sm-10">
                  <textarea name="key" placeholder="{{ entry_key }}" rows="5" id="input-key" class="form-control mb-1">{{ key }}</textarea>
                  <button type="button" id="button-generate" class="btn btn-primary"><i class="fas fa-sync"></i> {{ button_generate }}</button>
                  <div id="error-key" class="invalid-feedback"></div>
                </div>
              </div>
              <div class="row mb-3">
                <label for="input-status" class="col-sm-2 col-form-label">{{ entry_status }}</label>
                <div class="col-sm-10">
                  <select name="status" id="input-status" class="form-select">
                    <option value="1"{% if status %} selected{% endif %}>{{ text_enabled }}</option>
                    <option value="0"{% if not status %} selected{% endif %}>{{ text_disabled }}</option>
                  </select>
                </div>
              </div>
            </div>
            <div id="tab-ip" class="tab-pane">
              <div class="alert alert-info"><i class="fas fa-info-circle"></i> {{ text_ip }}</div>
              <div class="table-responsive">
                <table id="ip" class="table table-bordered table-hover">
                  <thead>
                    <tr>
                      <td class="text-start">{{ entry_ip }}</td>
                      <td class="text-end"></td>
                    </tr>
                  </thead>
                  <tbody>
                    {% set ip_row = 0 %}
                    {% for api_ip in api_ips %}
                      <tr id="ip-row-{{ ip_row }}">
                        <td class="text-start"><input type="text" name="api_ip[]" value="{{ api_ip }}" placeholder="{{ entry_ip }}" class="form-control"/></td>
                        <td class="text-end"><button type="button" onclick="$('#ip-row-{{ ip_row }}').remove()" data-bs-toggle="tooltip" title="{{ button_remove }}" class="btn btn-danger"><i class="fas fa-minus-circle"></i></button></td>
                      </tr>
                      {% set ip_row = ip_row + 1 %}
                    {% endfor %}
                  </tbody>
                  <tfoot>
                    <tr>
                      <td></td>
                      <td class="text-end"><button type="button" id="button-ip" data-bs-toggle="tooltip" title="{{ button_ip_add }}" class="btn btn-primary"><i class="fas fa-plus-circle"></i></button></td>
                    </tr>
                  </tfoot>
                </table>
              </div>
            </div>
            <div id="tab-session" class="tab-pane">
              <div class="table-responsive">
                <table id="session" class="table table-bordered table-hover">
                  <thead>
                    <tr>
                      <td class="text-start">{{ column_token }}</td>
                      <td class="text-start">{{ column_ip }}</td>
                      <td class="text-start">{{ column_date_added }}</td>
                      <td class="text-start">{{ column_date_modified }}</td>
                      <td class="text-end">{{ column_action }}</td>
                    </tr>
                  </thead>
                  <tbody>
                    {% if api_sessions %}
                      {% for api_session in api_sessions %}
                        <tr>
                          <td class="text-start">{{ api_session.session_id }}</td>
                          <td class="text-start">{{ api_session.ip }}</td>
                          <td class="text-start">{{ api_session.date_added }}</td>
                          <td class="text-start">{{ api_session.date_modified }}</td>
                          <td class="text-end"><button type="button" value="{{ api_session.api_session_id }}" data-bs-toggle="tooltip" title="{{ button_remove }}" class="btn btn-danger"><i class="fas fa-minus-circle"></i></button></td>
                        </tr>
                      {% endfor %}
                    {% else %}
                      <tr>
                        <td class="text-center" colspan="5">{{ text_no_results }}</td>
                      </tr>
                    {% endif %}
                  </tbody>
                </table>
              </div>
            </div>
          </div>
          <input type="hidden" name="api_id" value="{{ api_id }}" id="input-api-id"/>
        </form>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
$('#button-generate').on('click', function() {
    rand = '';

    string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

    for (i = 0; i < 256; i++) {
        rand += string[Math.floor(Math.random() * (string.length - 1))];
    }

    $('#input-key').val(rand);
});
//--></script>
<script type="text/javascript"><!--
var ip_row = {{ ip_row }};

$('#button-ip').on('click', function() {
    html = '<tr id="ip-row-' + ip_row + '">';
    html += '  <td class="text-end"><input type="text" name="api_ip[]" value="" placeholder="{{ entry_ip }}" class="form-control"/></td>';
    html += '  <td class="text-end"><button type="button" onclick="$(\'#ip-row-' + ip_row + '\').remove();" data-bs-toggle="tooltip" title="{{ button_remove }}" class="btn btn-danger"><i class="fas fa-minus-circle"></i></button></td>';
    html += '</tr>';

    $('#ip tbody').append(html);

    ip_row++;
});
//--></script>
<script type="text/javascript"><!--
$('#session button').on('click', function(e) {
    e.preventDefault();

    var element = this;

    $.ajax({
        url: 'index.php?route=user/api|deletesession&user_token={{ user_token }}&api_session_id=' + $(element).val(),
        type: 'post',
        dataType: 'json',
        beforeSend: function() {
            $(element).button('loading');
        },
        complete: function() {
            $(element).button('reset');
        },
        success: function(json) {
            $('.alert-dismissible').remove();

            if (json['error']) {
                $('#tab-session').prepend('<div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
            }

            if (json['success']) {
                $('#tab-session').prepend('<div class="alert alert-success alert-dismissible"><i class="fas fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');

                $(element).parent().parent().remove();
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            oc.alert('danger', thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});
//--></script>
{{ footer }}